package formServlets;
import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreAttributes;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import java.io.IOException;
import java.util.Iterator;

//import guestbook.PMF;

public class addUserToDatabase extends HttpServlet  {

	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		getServletContext().getRequestDispatcher("SNSP.html").forward(req, resp);
	}
 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String username = request.getParameter("Username");
		// System.out.println(username);
		
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		/* This Try/Catch code tests to see if the username is already in the database
		 * If we can get a key, then the username is already there, then we redirect to a 
		 * page telling us that the username exists.
		 * If we hit the exception we assume that no username exists and the rest of the code runs.		 * 
		 */
		try {
			Key key = KeyFactory.createKey("UserSignUp", username);
			Entity e=datastore.get(key);
			System.out.println("got key successfully, username already exists");
			response.sendRedirect("test/UsernameExists.html");
			} catch (EntityNotFoundException e) {
				// TODO Auto-generated catch block
				System.out.println("caught exception");
			}
					
		String password = request.getParameter("Password");
		String email = request.getParameter("Email");
		String seekingDegree = request.getParameter("SeekingDegree");
		String previousGradSchool = request.getParameter("PreviousGradSchool");
		String graduateGPA = request.getParameter("graduateGPA");
		String greScore = request.getParameter("GREscore");
		String previousUndergrad = request.getParameter("PreviousUndergrad");
		String major = request.getParameter("major");
		String highSchool = request.getParameter("highschool");
		String satScore = request.getParameter("SATscore");
	
		Entity user = new Entity("UserSignUp",username);
		user.setProperty("Username", username);
		user.setProperty("Password", password);
		user.setProperty("email", email);
		user.setProperty("seekingDegree", seekingDegree);
		user.setProperty("previousGradSchool", previousGradSchool);
		user.setProperty("graduateGPA", graduateGPA);
		user.setProperty("greScore", greScore);
		user.setProperty("previousUndergrad",previousUndergrad);
		user.setProperty("major", major);
		user.setProperty("highSchool", highSchool);
		user.setProperty("satScore", satScore);
		
		datastore.put(user);
		
		System.out.println(user.getProperty("Username").toString());
		System.out.println(user.getProperty("seekingDegree").toString());
		System.out.println(user.getProperty("previousGradSchool").toString());
		// Back to original test page after the user has been successfully added.
		response.sendRedirect("testDatabasepage.html");
		 
	}
 
}